package com.two.service.businessman.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.two.common.entity.Restaurant;
import com.two.common.util.CommonResult;
import com.two.common.vo.RestaurantVo;
import com.two.service.businessman.openFeign.RestaurantOpenFeign;
import com.two.service.businessman.openFeign.UserOpenFeign;
import com.two.service.businessman.service.BusinessmanService;
import com.two.service.businessman.vo.BusinessmanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/businessman/businessman")
public class BusinessmanController {

    @Autowired
    private BusinessmanService businessmanService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RestaurantOpenFeign restaurantOpenFeign;

    @Autowired
    private UserOpenFeign userOpenFeign;

    //查询待审核的商家入驻
    @PostMapping("/selectByState")
    public CommonResult selectByState(){
        return businessmanService.selectByState();
    }

    //申请商家入驻  往businessman表添加state为0的数据
    @PostMapping("/insertBusinessman")
    public CommonResult insertBusinessman(@RequestBody BusinessmanVo businessmanVo, HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = stringRedisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(token1, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String userid = user.getString("userid");
        //System.out.println("用户id========="+userid);
        businessmanVo.setUserid(userid);
        return businessmanService.insertBusinessman(businessmanVo);
    }

    //审核通过
    @PostMapping("/pass")
    @Transactional
    public CommonResult pass(@RequestBody BusinessmanVo businessmanVo){
        //调用restaurant服务添加功能
        RestaurantVo restaurantVo = new RestaurantVo();
        restaurantVo.setRestaurantName(businessmanVo.getRestaurantname());
        restaurantVo.setAddress(businessmanVo.getAddress());
        restaurantVo.setRemark(businessmanVo.getRemark());
        restaurantVo.setPhone(businessmanVo.getPhone());
        restaurantVo.setUrl(businessmanVo.getUrl());
        Integer rid = restaurantOpenFeign.addRestaurant(restaurantVo);
        if (rid!=0){
            //修改用户表rid和roleid
            Integer userid = Integer.parseInt(businessmanVo.getUserid());
            Integer updateRidAndRoleid = userOpenFeign.updateRidAndRoleid(rid, userid);
            if (updateRidAndRoleid==1){
                //修改商户信息表的state为1
                return businessmanService.updateStateById(businessmanVo);
            }
        }
        return new CommonResult(5000,"审核通过失败,请联系管理员",null);
    }

    //审核驳回
    @PostMapping("/rebut")
    public CommonResult updateState(@RequestBody BusinessmanVo businessmanVo){
        return businessmanService.updateState(businessmanVo);
    }
}
